﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Classes
{
    class GraphImpl : IGraphImpl
    {
        IList<City> cities;
        IList<Flight> lol;

        public GraphImpl()
        {
            lol = new List<Flight>();
            cities = new List<City>();
        }
        public bool IsEmpty()
        {
            return cities.Count == 0;
        }
        public void AddCity(City c)
        {
            cities.Add(c);
        }
        public void AddFlight(City from, City to)
        {	//PRE (to,from) is not contained in the graph

            from.AddFlight(to);
        }

        public bool ContainsCity(City city)
        {
            return cities.Contains(city);
        }

        public bool IsAdjacent(City startCity, City endCity)
        {
            bool isAdjacent = false;
            foreach (Flight f in startCity.Flights())
            {
                if (f.To == endCity)
                {
                    isAdjacent = true;
                }
            }
            return isAdjacent;

        }

        public IList<City> GetAdjacencies(City city)
        {
            IList<City> list = new List<City>();
            foreach (Flight f in city.Flights())
            {
                list.Add(f.To);
            }
            return list;

        }


        /* I don't know how to implement this shit and when to use it
         public IEnumerator<City> Cities()
         {
             throw new System.NotImplementedException();
         }
         */

        public int GetNoOfCities()
        {
            int i = cities.Count;
            return i;
        }

        public int GetNoOfFlights()
        {
            int numberOfFlights = 0;
            foreach (City c in this.cities)
            {

                numberOfFlights += c.Flights().Count;
            }
            return numberOfFlights;
        }

        public void Clear()
        {
            cities = new List<City>();
        }

        /*      public void Bfs(City v)
              {
                  throw new System.NotImplementedException();

              }*/

        public void Dfs(City v)//normal version
        {
            v.Mark = true;
            Console.WriteLine(v.Name + " " + v.Country);
            ICollection<City> adj = GetAdjacencies(v);
            foreach (City u in adj)
            {
                if (u.Mark != true)
                {
                    Dfs(u);
                }
            }
        }

        public void Connection(City from, City to)
        {
            IList<Flight> flightList = new List<Flight>();
            from.Mark = true;
            IList<City> adjencecies = GetAdjacencies(from);
            foreach (City u in adjencecies)
            {
                if (!u.Equals(to))
                {
                        Flight flight = new Flight(from, u);
                        flightList.Add(flight);
                        ConnectionCon(u, to, flightList);
                }

                else
                {
                    Flight flight = new Flight(from, u);
                    flightList.Add(flight);
                    returnAll(flightList);
                }


            }

           
        }
        //Nuclear
        public void ConnectionCon(City from, City to, IList<Flight> flightList)
        {
            from.Mark = true;
            IList<City> adjencecies = GetAdjacencies(from);
            foreach (City u in adjencecies)
            {
                if (!u.Equals(to))
                {
                    if (u.Mark != true)
                    {
                        Flight flight = new Flight(from, u);
                        flightList.Add(flight);
                        ConnectionCon(u, to, flightList);
                        flightList.RemoveAt(flightList.Count-1);
                    }
                }

                else
                {
                    Flight flight = new Flight(from, u);
                    flightList.Add(flight);
                    returnAll(flightList);
                }


            }

            
        }
        private void returnAll(IList<Flight> con)
        {
            foreach (Flight u in con)
            {
                lol.Add(u);
                Console.WriteLine(u.From.Name + " ---> " + u.To.Name);
            }
            

        }

        private bool itIsTheLast(City c) { 
           bool isLast = true;
           ICollection<City> adji = GetAdjacencies(c);
           foreach (City item in adji)
           {
               if (!item.Mark)
               {
                   isLast = false;
               }
              
           }


           return isLast;
        }

        public IList<City> GetCitiesConnection(City city)
        {
            IList<City> listOfCities = new List<City>();
            city.Mark = true;
            listOfCities.Add(city);
            Console.WriteLine(city.Name);
            Console.ReadLine();
            ICollection<City> adj = GetAdjacencies(city);
            foreach (City u in adj)
            {
                if (u.Mark != true)
                {
                    GetCitiesConnection(u);
                }
            }
            return listOfCities;
        }


    }
}

       /*
    public void Unmark()
        {
            throw new System.NotImplementedException();
        }
       */

    


  